<?php
	
	class UserBusiness {
			
		public function save($user, $dataBaseConnection = null) {
			
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$userDataAccess = DataAccessFactory::factory('UserDataAccessMySQL');
			
			$result = $userDataAccess->save($user, $dataBaseConnection);
			
			if ($connectionOpened) {
				if ($result == -1) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		public function select($user) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$userDataAccess = DataAccessFactory::factory('UserDataAccessMySQL');

			$result = $userDataAccess->select($user, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
		
		public function selectFromSession($user) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$userDataAccess = DataAccessFactory::factory('UserDataAccessMySQL');
			
			$result = $userDataAccess->selectFromSession($user, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
	}

?>